package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {

    @Autowired
    private AliOSSUtils aliOSSUtils;

//    //本地存储文件
//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传{}，{}，{}",username,age,image);
//
//        //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//
//        //构造唯一文件名 uuid(通用唯一识别码)
//        int index = originalFilename.lastIndexOf(".");
//        String extname = originalFilename.substring(index); //截取拓展名
//        String newFileName = UUID.randomUUID().toString() + extname; //获取新的文件名
//        log.info("获取新的文件名：{}",newFileName);
//
//        System.out.println(originalFilename);
//        //将文件存储在服务器的磁盘目录中
//        image.transferTo(new File("D:\\AHeiMa\\deplop\\images\\"+newFileName));
//
//        return Result.success();
//    }

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传,{}",image.getOriginalFilename());
        String url = aliOSSUtils.upload(image); //文件访问的路径
        return Result.success(url);
    }
}
